import {
	reactive
} from "vue";
import {
	mobileCheck,
	msg
} from "./common";
import {
	sendCode
} from "../api/module/login";
// 验证码
export const loginCode = reactive({
	text: '获取验证码',
	countDownNumber: 60,
	isStart: false,
	interval: null,
	sendCode: (cellphone, type) => {
		if (!cellphone) {
			return msg('请输入手机号');
		}
		if (!mobileCheck(cellphone)) {
			return msg('手机号格式有误');
		}
		if (loginCode.isStart) return
		sendCode({
			mobile: cellphone,
			type: type || 'login_code',
		}).then(res => {
			console.log(res);
			loginCode.countDown()
		})
	},
	clear: () => {
		loginCode.countDownNumber = 60
		loginCode.text = '重新发送'
		loginCode.isStart = false
		clearInterval(loginCode.interval)
	},
	countDown: () => {
		loginCode.countDownNumber = 60;
		loginCode.isStart = true
		loginCode.interval = setInterval(() => {
			if (loginCode.countDownNumber == 1) {
				loginCode.text = '重新发送'
				loginCode.isStart = false
				clearInterval(loginCode.interval)
				return
			}
			loginCode.countDownNumber--
			loginCode.text = `${loginCode.countDownNumber}s后重新发送`
		}, 1000)
	}
});